package com.bshark.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bshark.biz.ContextService;
import com.bshark.util.JsonParser;

public class AppAction extends HttpServlet {

	private static final long serialVersionUID = -500970642865726178L;
	ContextService contextService = new ContextService();

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 获取数据
		String data = request.getParameter("data");
		if (data == null || "".equals(data)) {
			return;
		}
		// 获取命令行
		String str_cmd = JsonParser.get(data, "cmd");
		if (str_cmd == null || "".equals(str_cmd)) {
			return;
		}
		int cmd = Integer.valueOf(str_cmd);
		PrintWriter out = response.getWriter();
		String resp = "";
		switch (cmd) {
		// 获取文章
		case 1:
			resp = contextService.getContext(data);
			break;
		// 获取评论
		case 3:
			resp = contextService.getComment(data);
			break;
		// 投稿
		case 2:
			resp =contextService.addContext(data);
			break;
		// 评论
		case 4:
			resp =contextService.addComment(data);
			break;
		default:
			break;
		}
		out.println(resp);
	}
}
